package com.example.ymm.modules1.service.impl.hl.ex;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.hl.ex.ExTeamUser;
import com.example.ymm.modules1.mapper.ex.ExTeamUserMapper;
import com.example.ymm.modules1.query.hl.ex.ExTeamUserQueryCriteria;
import com.example.ymm.modules1.service.dto.heng.ex.RankItemDto;
import com.example.ymm.modules1.service.hl.ex.ExTeamUserService;
import com.example.ymm.util.QueryHelpPlus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
//@Transactional
@Slf4j
@DS("slave2")
public class ExTeamUserServiceImpl extends BaseServiceImpl<ExTeamUserMapper, ExTeamUser> implements ExTeamUserService {

    @Override
    public TableList<ExTeamUser> list(ExTeamUserQueryCriteria criteria, YmPageDto pageDto) {
        IPage<ExTeamUser> page = new Page<ExTeamUser>(pageDto.getPage(),pageDto.getPageSize());
        QueryWrapper predicate = QueryHelpPlus.getPredicate(ExTeamUser.class, criteria);
//        predicate.orderByDesc("create_time");
        baseMapper.selectPage(page, predicate);
        TableList<ExTeamUser> tableList=new TableList(page);
        return tableList;
    }

    public List<ExTeamUser> queryAll(ExTeamUserQueryCriteria criteria) {
        QueryWrapper predicate = QueryHelpPlus.getPredicate(ExTeamUser.class, criteria);
        predicate.orderByAsc("sort");
        List<ExTeamUser> result =baseMapper.selectList(predicate);
        return result;
    }

    @Override
    public ExTeamUser findByUserName(String userName) {
        ExTeamUserQueryCriteria criteria=new ExTeamUserQueryCriteria();
        criteria.setUserName(userName);
        List<ExTeamUser> exTeamUsers = queryAll(criteria);
        ExTeamUser exTeamUser=null;
        if(exTeamUsers.size()>0){
            exTeamUser=exTeamUsers.get(0);
        }
        return exTeamUser;
    }

    @Override
    public List<RankItemDto> getRanks(String type) {
        List<RankItemDto> rankTeam =new ArrayList<>();
        if("团队".equals(type)){
             rankTeam = baseMapper.getRankTeam();
        }else{
            rankTeam =baseMapper.getRankUser();
        }
        return rankTeam;
    }

    @Override
    public void testSort() {
        List<ExTeamUser> exTeamUsers = queryAll(null);

        for (int i = 0; i < exTeamUsers.size(); i++) {
            ExTeamUser teamUser = exTeamUsers.get(i);
            teamUser.setSort(i+1);
        }
        saveOrUpdateBatch(exTeamUsers);
    }
}
